local suye = fk.CreateSkill{
  name = "xiaobai__suye",
}

Fk:loadTranslationTable{
  ["xiaobai__suye"] = "肃野",
  [":xiaobai__suye"] = "当你使用或响应伤害牌后，你可以令一名手牌数不为众数的角色将手牌数向众数摸或弃置一张，"..
    "若其手牌数因此变为众数，你摸两张牌。",
  ["#xiaobai__suye_draw"] = "摸一张牌",
  ["#xiaobai__suye_discard"] = "弃一张牌",
  ["#xiaobai__suye_both"] = "摸或弃",
  ["#xiaobai__suye_modes"] = "众数",
  ["#xiaobai__suye_prompt"] = "肃野：你可以令一名手牌数不为众数的角色将手牌数向众数摸或弃置一张（众数：%arg）"

}

Fk:addTargetTip{
  name = "xiaobai__suye",
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    local modes = extra_data.extra_data
    local num = to_select:getHandcardNum()
    if table.contains(modes, num) then
      return "#xiaobai__suye_modes"
    elseif num < modes[1] then
      return "#xiaobai__suye_draw"
    elseif num > modes[#modes] then
      return "#xiaobai__suye_discard"
    else
      return "#xiaobai__suye_both"
    end
  end
}

local skill_spec = {
  can_trigger = function (self, event, target, player, data)
    local judge = false
    if event == fk.CardUsing then
      judge = (target == player and player:hasSkill(self) and data.card.is_damage_card)
    elseif event == fk.CardRespondFinished then
      judge = (target == player and player:hasSkill(self) and data.responseToEvent.card.is_damage_card)
    elseif event == fk.CardUseFinished then
      judge = (target == player and player:hasSkill(self) and data.responseToEvent and data.responseToEvent.card and data.responseToEvent.card.is_damage_card)
    end
    if judge then
      local modes = {}
      local card_num_nums = {}
      local room = player.room;
      local max_card_num = 0
      for _, p in ipairs(room.alive_players) do
        local cn = p:getHandcardNum()
        if cn ~= 0 then
          card_num_nums[cn] = (card_num_nums[cn] or 0) + 1
          if card_num_nums[cn] > max_card_num then
            max_card_num = card_num_nums[cn]
          end
        end
      end
      local targets = table.filter(room.alive_players, function (p)
      return not table.contains(modes, p:getHandcardNum())
    end)
      if max_card_num == 1 or #targets == 0 then return end
      for cn, cnn in pairs(card_num_nums) do
        if cnn == max_card_num then
          table.insert(modes, cn)
        end
      end
      table.sort(modes)
      event:setCostData(self, {xiaobai__suye = modes})
      return true
    end
  end,
  on_cost = function (self, event, target, player, data)
    local modes = event:getCostData(self).xiaobai__suye
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not table.contains(modes, p:getHandcardNum())
    end)
    local t = room:askToChoosePlayers(player, {
      max_num = 1,
      min_num = 1,
      prompt = "#xiaobai__suye_prompt:::"..table.concat(modes, ', '),
      targets = targets,
      skill_name = suye.name,
      target_tip_name = "xiaobai__suye",
      extra_data = modes
    })
    if #t > 0 then
      event:setCostData(self, {target = t[1], xiaobai__suye = modes})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local t = event:getCostData(self).target ---@type ServerPlayer
    local num = t:getHandcardNum()
    local modes = event:getCostData(self).xiaobai__suye
    local room = player.room
    if num < modes[1] then
      t:drawCards(1, suye.name)
    elseif num > modes[#modes] then
      room:askToDiscard(t, {
        min_num = 1,
        max_num = 1,
        cancelable = false,
        skill_name = suye.name,
      })
    else
      local choice = room:askToChoice(player, {
        choices = {
          "#xiaobai__suye_draw",
          "#xiaobai__suye_discard"
        },
        cancelable = false,
        skill_name = suye.name
      })
      if choice == "#xiaobai__suye_draw" then
        t:drawCards(1, suye.name)
      else
        room:askToDiscard(t, {
        min_num = 1,
        max_num = 1,
        cancelable = false,
        skill_name = suye.name,
      })
      end
    end
    if player.dead then return end
    for _, n in ipairs(modes) do
      if t:getHandcardNum() == n then
        player:drawCards(2, suye.name)
        return
      end
    end
  end
}

suye:addEffect(fk.CardRespondFinished, skill_spec)


suye:addEffect(fk.CardUseFinished, skill_spec)

suye:addEffect(fk.CardUsing, skill_spec)

return suye